﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SFP;

namespace LevelEditor
{
    public partial class GameObjectPreferencesForm : Form
    {
        private LevelEditorForm levelEditorForm;
        private GameObject gameObject;
        private Size resolution;
        private LayerLE layerLE;

        private int posX = 0;
        private int posY = 0;
        private int columns = 1;
        private int rows = 1;

        public GameObjectPreferencesForm(LevelEditorForm levelEditorForm)
        {
            InitializeComponent();
            this.levelEditorForm = levelEditorForm;
            this.gameObject = levelEditorForm.GetSelectedGameObject();
            this.resolution = levelEditorForm.GetResolution();
            this.layerLE = levelEditorForm.GetActiveLayerLE();

            gameObjNameLabel.Text = gameObject.Name;
            gameObjTypeLabel.Text = gameObject.Type;
            posX = (int)(gameObject.X + layerLE.GetLayer().getXScroll());
            posXTextBox.Text = "" + posX;
            posY = (int)(resolution.Height - (gameObject.Y - layerLE.GetLayer().getYScroll()));
            posYTextBox.Text = "" + posY;
            columns = gameObject.GroupColumns;
            columnTextBox.Text = "" + columns;
            rows = gameObject.GroupRows;
            rowsTextBox.Text = "" + rows;
            
            levelEditorForm.SetDialogFormOpen(true);
        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void applyButton_Click(object sender, EventArgs e)
        {
            gameObject.X = posX - layerLE.GetLayer().getXScroll();
            gameObject.Y = (resolution.Height - posY) + layerLE.GetLayer().getYScroll();
            gameObject.GroupColumns = columns;
            gameObject.GroupRows = rows;

            Close();
        }

        private void posXTextBox_TextChanged(object sender, EventArgs e)
        {
            processIntegerTextBoxChange(posXTextBox, ref posX);
        }

        private void posYTextBox_TextChanged(object sender, EventArgs e)
        {
            processIntegerTextBoxChange(posYTextBox, ref posY);
        }

        private void columnTextBox_TextChanged(object sender, EventArgs e)
        {
            processIntegerTextBoxChange(columnTextBox, ref columns);
        }

        private void rowsTextBox_TextChanged(object sender, EventArgs e)
        {
            processIntegerTextBoxChange(rowsTextBox, ref rows);
        }

        private void processIntegerTextBoxChange(TextBox textBox, ref int param)
        {
            if ("".Equals(textBox.Text)) return;

            try
            {
                param = int.Parse(textBox.Text);
            }
            catch
            {
                textBox.Text = "" + param;
            }
        }

        private void posXTextBox_Leave(object sender, EventArgs e)
        {
            processIntegerTextBoxLeave(posXTextBox, ref posX);
        }

        private void posYTextBox_Leave(object sender, EventArgs e)
        {
            processIntegerTextBoxLeave(posYTextBox, ref posY);
        }

        private void columnTextBox_Leave(object sender, EventArgs e)
        {
            processIntegerTextBoxLeave(columnTextBox, ref columns);
        }

        private void rowsTextBox_Leave(object sender, EventArgs e)
        {
            processIntegerTextBoxLeave(rowsTextBox, ref rows);
        }

        private void processIntegerTextBoxLeave(TextBox textBox, ref int param)
        {
            textBox.Text = "" + param;
        }

        private void GameObjectPreferencesForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            levelEditorForm.SetDialogFormOpen(false);
        }
    }
}
